/**
 * 滚动到指定的单元格位置
 */
export function scrollToCell(
    rowIndex: number,
    columnIndex: number,
    containerRef: React.RefObject<HTMLElement>,
    rowHeight: number = 36,
    columns?: Array<{ width?: number }>
) {
    if (!containerRef.current) return;

    const container = containerRef.current;
    const containerRect = container.getBoundingClientRect();

    // 计算目标位置
    const targetTop = rowIndex * rowHeight;

    // 计算列位置（考虑每列的实际宽度）
    let targetLeft = 0;
    if (columns) {
        for (let i = 0; i < columnIndex; i++) {
            targetLeft += columns[i]?.width || 120; // 默认列宽120px
        }
    } else {
        targetLeft = columnIndex * 120; // 默认列宽
    }

    // 计算单元格在容器中的相对位置
    const cellTopInContainer = targetTop - container.scrollTop;
    const cellLeftInContainer = targetLeft - container.scrollLeft;

    // 检查单元格是否已经在可视区域中间80%的位置
    const visibleHeight = containerRect.height;
    const visibleWidth = containerRect.width;
    const centerTop = visibleHeight / 2;
    const centerLeft = visibleWidth / 2;
    const toleranceTop = visibleHeight * 0.4; // 中间80%的高度范围
    const toleranceLeft = visibleWidth * 0.4; // 中间80%的宽度范围

    const isInCenterVertical = Math.abs(cellTopInContainer - centerTop) <= toleranceTop;
    const isInCenterHorizontal = Math.abs(cellLeftInContainer - centerLeft) <= toleranceLeft;

    // 如果单元格已经在中间80%区域，不需要滚动
    if (isInCenterVertical && isInCenterHorizontal) {
        return;
    }

    // 计算需要滚动的位置（将目标单元格滚动到容器中央）
    const newScrollTop = Math.max(0, targetTop - containerRect.height / 2);
    const newScrollLeft = Math.max(0, targetLeft - containerRect.width / 2);

    // 平滑滚动到目标位置
    container.scrollTo({
        top: newScrollTop,
        left: newScrollLeft,
        // behavior: 'smooth',
    });
}

/**
 * 滚动到指定的单元格位置（支持动态行高）
 */
export function scrollToCellWithDynamicHeight(
    rowIndex: number,
    columnIndex: number,
    containerRef: React.RefObject<HTMLElement>,
    rowHeights: Map<number, number>,
    defaultRowHeight: number = 36,
    columns?: Array<{ width?: number }>
) {
    if (!containerRef.current) return;

    const container = containerRef.current;
    const containerRect = container.getBoundingClientRect();

    // 计算目标位置（考虑动态行高）
    let targetTop = 0;
    for (let i = 0; i < rowIndex; i++) {
        targetTop += rowHeights.get(i) ?? defaultRowHeight;
    }

    // 计算列位置（考虑每列的实际宽度）
    let targetLeft = 0;
    if (columns) {
        for (let i = 0; i < columnIndex; i++) {
            targetLeft += columns[i]?.width || 120; // 默认列宽120px
        }
    } else {
        targetLeft = columnIndex * 120; // 默认列宽
    }

    // 计算单元格在容器中的相对位置
    const cellTopInContainer = targetTop - container.scrollTop;
    const cellLeftInContainer = targetLeft - container.scrollLeft;

    // 检查单元格是否已经在可视区域中间80%的位置
    const visibleHeight = containerRect.height;
    const visibleWidth = containerRect.width;
    const centerTop = visibleHeight / 2;
    const centerLeft = visibleWidth / 2;
    const toleranceTop = visibleHeight * 0.4; // 中间80%的高度范围
    const toleranceLeft = visibleWidth * 0.4; // 中间80%的宽度范围

    const isInCenterVertical = Math.abs(cellTopInContainer - centerTop) <= toleranceTop;
    const isInCenterHorizontal = Math.abs(cellLeftInContainer - centerLeft) <= toleranceLeft;

    // 如果单元格已经在中间80%区域，不需要滚动
    if (isInCenterVertical && isInCenterHorizontal) {
        return;
    }

    // 计算需要滚动的位置（将目标单元格滚动到容器中央）
    const newScrollTop = Math.max(0, targetTop - containerRect.height / 2);
    const newScrollLeft = Math.max(0, targetLeft - containerRect.width / 2);

    // 平滑滚动到目标位置
    container.scrollTo({
        top: newScrollTop,
        left: newScrollLeft,
        // behavior: 'smooth',
    });
}

/**
 * 滚动到指定的行

export function scrollToRow(rowIndex: number, containerRef: React.RefObject<HTMLElement>, rowHeight: number = 36) {
    if (!containerRef.current) return;

    const container = containerRef.current;
    const containerRect = container.getBoundingClientRect();

    // 计算目标位置
    const targetTop = rowIndex * rowHeight;

    // 计算需要滚动的位置（将目标行滚动到容器中央）
    const newScrollTop = Math.max(0, targetTop - containerRect.height / 2);

    // 平滑滚动到目标位置
    container.scrollTo({
        top: newScrollTop,
        behavior: 'smooth',
    });
}
 */

/**
 * 滚动到指定的列
export function scrollToColumn(
    columnIndex: number,
    containerRef: React.RefObject<HTMLElement>,
    columnWidth: number = 120
) {
    if (!containerRef.current) return;

    const container = containerRef.current;
    const containerRect = container.getBoundingClientRect();

    // 计算目标位置
    const targetLeft = columnIndex * columnWidth;

    // 计算需要滚动的位置（将目标列滚动到容器中央）
    const newScrollLeft = Math.max(0, targetLeft - containerRect.width / 2);

    // 平滑滚动到目标位置
    container.scrollTo({
        left: newScrollLeft,
        behavior: 'smooth',
    });
}
 */
